<template>
  <div>
    <el-row>
      <el-card class="box-card">
        <el-row>
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品类别管理</el-breadcrumb-item>
          </el-breadcrumb>
        </el-row>
        <el-row><br><br></el-row>
        <el-row>
          <el-col :span="1"><br></el-col>
          <el-col :span="5">
            <el-input v-model="dto.name" placeholder="请输入内容"></el-input>
          </el-col>
          <el-col :span="1">
            <el-button type="primary" icon="el-icon-search" @click="getcategoryList()">搜索</el-button>
          </el-col>
          <el-col :span="5">
            <el-button type="success" round @click="HandleAdd()">添加主类别</el-button>
          </el-col>
        </el-row>
        <el-row><br><br></el-row>
        <el-row>
          <el-col :span="1"><br></el-col>
          <el-col :span="14">
            <el-table
              :data="categoryList"
              style="width: 100%">
              <el-table-column
                label="序号"
                type="index"
                width="50">
              </el-table-column>
              <el-table-column
                prop="name"
                label="类型"
                width="120">
              </el-table-column>
              <el-table-column
                label="状态"
                width="120">
                <template slot-scope="scope">
                  <span style="color: green;" v-if="scope.row.status == 1">启用</span>
                  <span style="color: red;" v-else>停用</span>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini" type="primary" icon="el-icon-edit"
                    @click="handleEdit(scope.row)">编辑</el-button>
                  <el-button
                    size="mini" type="info" icon="el-icon-info"
                    @click="handleInfo(scope.row)">查看详细</el-button>
                  <el-button
                    size="mini" v-if="scope.row.status == 1" type="warning"
                    @click="handleDisable(scope.row)">停用</el-button>
                  <el-button
                    size="mini" v-if="scope.row.status == 0" type="success"
                    @click="handleEnable(scope.row)">启用</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="1"><br></el-col>
          <el-col :span="5">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="dto.page"
              :page-size="5"
              layout="total, prev, pager, next"
              :total="total">
            </el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </el-row>

    <!-- 添加主类别的表单弹出框 -->
    <el-dialog :title="formtitle" :visible.sync="dialogCategoryVisible" style="width: 80%;">
      <el-form :model="category" :rules="rules" ref="ruleForm">
        <el-form-item label="类别名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="addCategory('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 查看子类别列表的弹出框 -->
    <el-dialog title="子类别管理" :visible.sync="dialogTableVisible">
      <el-col :span="1">
        <el-button type="success" round @click="HandleAddSub()">添加子类别</el-button>
      </el-col>
      <el-table :data="subcategoryList">
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column property="name" label="类型" width="150"></el-table-column>
        <el-table-column
          label="状态"
          width="120">
          <template slot-scope="scope">
            <span style="color: green;" v-if="scope.row.status == 1">启用</span>
            <span style="color: red;" v-else>停用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini" type="primary" icon="el-icon-edit"
              @click="handleEditSub(scope.row)">编辑</el-button>
            <el-button
              size="mini" v-if="scope.row.status == 1" type="warning"
              @click="handleDisable(scope.row)">停用</el-button>
            <el-button
              size="mini" v-if="scope.row.status == 0" :disabled="status !== 1" type="success"
              @click="handleEnable(scope.row)">启用</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleSubCurrentChange"
            :current-page.sync="subDto.page"
            :page-size="5"
            layout="total, prev, pager, next"
            :total="subTotal">
          </el-pagination>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入类别名称'));
      } else if (this.categoryName != '' && value === this.categoryName) {
        callback(new Error('类别名称未改变'));
      }{
        callback();
      }
    }
    return{
      dto:{
        name:"",
        page:1,
        size:5
      },
      subDto:{
        page:1,
        size:5,
      },
      status:"",
      categoryList:[],
      subcategoryList:[],
      total:"",
      subTotal:"",
      category:{
        id:"",
        name:"",
        pid:"",
        status:""
      },
      categoryName:"",
      formtitle:"",
      dialogCategoryVisible:false,
      dialogTableVisible:false,
      formLabelWidth: "80px",
      rules: {
        name: [
          { validator:checkName, trigger: 'blur' }
        ]
      }
    }
  },
  mounted(){
    this.getcategoryList()
  },
  methods:{
    handleCurrentChange(val){
      this.dto.page = val
      this.getcategoryList()
    },
    handleSubCurrentChange(val){
      this.subDto.page = val
      this.getSubCategoryList(this.category.pid)
    },
    getcategoryList(){
      this.$axios.get('/category/getCategoryList',{
        params:{
          name: this.dto.name,
          page: this.dto.page,
          size: this.dto.size
        }}).then(res=>{
        if (res.data.code == 200) {
          this.categoryList = res.data.data.data
          this.total = res.data.data.total
        }
      })
    },
    close(formName){
      this.$refs[formName].clearValidate();
      this.dialogCategoryVisible = false
      this.formtitle = ''
    },
    HandleAdd(){
        this.formtitle = "添加主类别"
        this.category = {
          id:"",
          name:"",
          pid:0,
          status:""
        }
      this.dialogCategoryVisible = true
    },
    HandleAddSub(){
      this.formtitle = "添加子类别"
      this.category.name=""
      this.dialogCategoryVisible = true
    },
    handleEdit(row){
      this.formtitle = "编辑主类别"
      this.category.id = row.id
      this.category.name = row.name
      this.categoryName = row.name
      this.dialogCategoryVisible = true
    },
    handleEditSub(row){
      this.formtitle = "编辑子类别"
      this.category.id = row.id
      this.category.name = row.name
      this.categoryName = row.name
      this.dialogCategoryVisible = true
    },
    addCategory(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formtitle.startsWith('添加')) {
            this.$axios.post('/category/addCategory',this.category).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '添加成功！',
                  type: 'success'
                });
                if (this.formtitle == "添加主类别"){
                  this.getcategoryList()
                }
                if (this.formtitle == "添加子类别"){
                  this.getSubCategoryList(this.category.pid)
                }
                this.dialogCategoryVisible = false
              }
            })
          }
          if (this.formtitle.startsWith('编辑')) {
            this.$axios.put('/category/editCategory',this.category).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '编辑成功！',
                  type: 'success'
                });
                if (this.formtitle == "编辑主类别"){
                  this.getcategoryList()
                }
                if (this.formtitle == "编辑子类别"){
                  this.getSubCategoryList(this.category.pid)
                }
                this.dialogCategoryVisible = false
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    handleInfo(row){
      this.dialogTableVisible = true
      this.formtitle = "添加子类别"
      this.category.pid = row.id
      this.status = row.status
      this.getSubCategoryList(row.id)
    },
    getSubCategoryList(id){
      this.$axios.get('/category/getSubCategoryList',{
        params:{
          page: this.subDto.page,
          size: this.subDto.size,
          pid: id
        }}).then(res=>{
        if (res.data.code == 200) {
          this.subcategoryList = res.data.data.data
          this.subTotal = res.data.data.total
        }
      })
    },
    handleDelete(row){
      this.$confirm('此操作将永久删除该类别, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(row)
        this.$axios.delete("/category/deleteCategory",{
          params:{
            id: row.id,
            pid: row.pid
          }
        }).then(res=>{
          if (res.data.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            if (row.pid == 0) {
              this.getcategoryList()
            } else {
              this.getSubCategoryList(row.id)
            }  
          }
          if (res.data.code == 500) {
            this.$message.error(res.data.message);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    handleEnable(row){
      console.log(row)
      this.$axios.put('/category/enableCategory/' + row.id).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message: '启用成功!'
          });
          if (row.pid == 0) {
            this.getcategoryList()
          } else {
            this.getSubCategoryList(row.pid)
          }
        }
      })
    },
    handleDisable(row){
      this.$axios.put('/category/disableCategory/',row).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message: '停用成功!'
          });
          if (row.pid == 0) {
            this.getcategoryList()
          } else {
            this.getSubCategoryList(row.pid)
          }
        }
      })
    },
  }
}
</script>
<style scoped>
.box-card {
  height: 79vh;
}
</style>